<?php

/* Retrieve Data
 *
 * Takes request for data, authenticates, sends data to client JavaScript
 */

require("./dbconnect.inc.php");

// Grab data
extract ($_POST);

// Process
mysql_connect($host,$dbuser,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");


///////////////////////////////////
// This gets the BFKey from filedata by title and username

// Grab data
$datagrab=sprintf("SELECT * FROM filedata WHERE title='%s' AND username='%s'", mysql_real_escape_string($title), mysql_real_escape_string($username));


$dataresult=mysql_query($datagrab);
$numdata=mysql_numrows($dataresult);

$j=0;
while($j < $numdata){
$bfkey=mysql_result($dataresult,$j,"bfkey");
$j++;
}





///////////////////////////////////////////////
// This chunk gets the data from data by title

// Grab data
$datagrab=sprintf("SELECT * FROM data WHERE title='%s'", mysql_real_escape_string($title));


$dataresult=mysql_query($datagrab);
$numdata=mysql_numrows($dataresult);

$j=0;
while($j < $numdata){
$title=mysql_result($dataresult,$j,"title");
$data=mysql_result($dataresult,$j,"data");
$j++;
}



///////////////////////////////////////////////
// This point forward gets the user key from users by username and password
	
// Grab user data
$usergrab=sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", mysql_real_escape_string($username), mysql_real_escape_string($password));


$userresult=mysql_query($usergrab);
$numuser=mysql_numrows($userresult);

$j=0;
while($j < $numuser){
$sk_e=mysql_result($userresult,$j,"sk_e");
$sk_n=mysql_result($userresult,$j,"sk_n");
$sk_d=mysql_result($userresult,$j,"sk_d");
$sk_p=mysql_result($userresult,$j,"sk_p");
$sk_q=mysql_result($userresult,$j,"sk_q");
$sk_dmp1=mysql_result($userresult,$j,"sk_dmp1");
$sk_dmq1=mysql_result($userresult,$j,"sk_dmq1");
$sk_coeff=mysql_result($userresult,$j,"sk_coeff");
$j++;
	}



/* $echostuff=sprintf("Title is: %s", $title);
echo($echostuff. "<br>");
$echostuff=sprintf("Data is: %s", $data);
echo($echostuff. "<br>");
echo("Key is: ");
echo($bfkey); */

//HERE: We will be writing out the HTML to display the boxes for entering the private key information.

?>

<html>
	<head>
		<title>Decrypt Post</title>
		<script language="Javascript" type="text/javascript" src="crypto/sha1.js"></script>
		<script language="Javascript" type="text/javascript" src="crypto/blowfish.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/jsbn.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/jsbn2.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/prng4.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/rng.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/rsa.js"></script>
		<script language="JavaScript" type="text/javascript" src="crypto/rsa2.js"></script>
		<script language="JavaScript" type="text/javascript" src="js/retrievedata.js"></script>
	</head>
	<body>
		<form>
			<table border = 0>
				<tr>
					<td align="right"><b>Title:</b></td>
					<td><?php echo $title; ?></td>
				</tr>

				<tr>
					<td align="right"><b>Data:</b></td>
					<td><textarea cols="30" rows="4" id="data" name="data"><?php echo $data; ?></textarea></td>
				</tr>

				<tr>
					<td align="right"><!--<b>Private Key: n:--></b></td>
					<td><input type="hidden" wrap="SOFT" cols="80" id="n" rows="4" value="<?php echo $sk_n; ?>"></td>
				</tr>

				<tr>
					<td align="right"><!--<b>e:</b>--></td>
					<td><input type ="hidden" id="e" value ="<?php echo $sk_e; ?>" size=30></td>
				</tr>

				<tr>
					<td align="right"><!--<b>d:</b>--></td>
					<td><input type="hidden" cols="80" rows="4" id="d" value="<?php echo $sk_d; ?>"></td>
				</tr>

				<tr>
					<td align="right"><!--<b>p:</b>--></td>
					<td><input type="hidden" cols="80" rows="2" id="p" value="<?php echo $sk_p; ?>"></td>
				</tr>
				
				<tr>
					<td align="right"><!--<b>q:</b>--></td>
					<td><input type="hidden" cols="80" rows="2" id="q" value="<?php echo $sk_q; ?>"></td>
				</tr>

				<tr>
					<td align="right"><!--<b>dmp1:</b>--></td>
					<td><input type="hidden" cols="80" rows="2" id="dmp1" value="<?php echo $sk_dmp1; ?>"></td>
				</tr>
				
				<tr>
					<td align="right"><!--<b>dmq1:</b>--></td>
					<td><input type="hidden" cols="80" rows="2" id="dmq1" value="<?php echo $sk_dmq1; ?>"></td>
				</tr>

				<tr>
					<td align="right"><!--<b>Coeff:</b>--></td>
					<td><input type="hidden" cols="80" rows="2" id="coeff" value="<?php echo $sk_coeff; ?>"></td>
				</tr>

				<tr>
					<td align="right">&nbsp;</td>
					<td><input type="button" value="Decrypt" onclick="decrypt()"></td>
				</tr>

				<!-- We need an invisible field to contain the encrypted Blowfish key. -->
				<input type="hidden" id="bfkey" name="bfkey" value="<?php echo $bfkey; ?>">
			</table>
		</form>
	</body>
</html>
